package atm.frame;

import atm.ATMGUIMain;
import atm.pojo.UserDO;
import atm.service.Impl.UserServiceImpl;
import atm.service.UserService;
import atm.util.DBUtil;
import atm.util.Util;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

public class TransferPanel extends JPanel implements ActionListener {
    private Icon transferImage = Util.getIcon("/image/pane.jpg");//转账按钮图
    private Icon backImage = Util.getIcon("/image/pane.jpg");//返回按钮图
    private ATMGUIMain frame;//顶级容器实例
    private List<UserDO> usersList;//用户信息集合实例
    private JButton backBtn;//返回按钮
    private JLabel jLabelBack;//返回文本
    private JLabel transferUserJLabel;//转账用户提示插件
    private JTextField iptTransferUser;//转账用户输入框
    private JLabel transferMoneyJLabel;//转账金额提示插件
    private JTextField iptTransferMoney;//转账金额输入框
    private JButton transferBtn;//转账按钮
    private JLabel transferJLabel;
    private double transferMoney;
    String otherAccount = null;
    private UserService userService = new UserServiceImpl();
    private Font font = new Font("宋体", Font.PLAIN, 20);

    public TransferPanel(ATMGUIMain frame) {
        this.frame = frame;
        //设置空布局和透明度
        this.setLayout(null);
        this.setOpaque(false);
        //转账用户提示插件
        transferUserJLabel = new JLabel("输入收款人账户：");
        transferUserJLabel.setBounds(240, 190, 150, 50);
        transferUserJLabel.setFont(font);
        this.add(transferUserJLabel);
        //转账用户输入框
        iptTransferUser = new JTextField();
        iptTransferUser.setBounds(380, 200, 300, 30);
        iptTransferUser.setFont(font);
        this.add(iptTransferUser);
        //转账金额提示插件
        transferMoneyJLabel = new JLabel("输入转账金额：");
        transferMoneyJLabel.setBounds(240, 240, 150, 50);
        transferMoneyJLabel.setFont(font);
        this.add(transferMoneyJLabel);
        //转账金额输入框
        iptTransferMoney = new JTextField();
        iptTransferMoney.setBounds(380, 250, 300, 30);
        iptTransferMoney.setFont(font);
        this.add(iptTransferMoney);
        //转账按钮
        transferJLabel = new JLabel("转  账");
        transferJLabel.setBounds(900, 220, 140, 50);
        transferJLabel.setFont(new Font("宋体", Font.BOLD, 25));
        transferJLabel.setForeground(Color.white);
        this.add(transferJLabel);
        transferBtn = new JButton(transferImage);
        transferBtn.setBounds(870, 220, 140, 50);
        transferBtn.setOpaque(false);
        this.add(transferBtn);
        transferBtn.addActionListener(this);
        //返回按钮
        jLabelBack = new JLabel("返  回");
        jLabelBack.setBounds(900, 300, 100, 50);
        jLabelBack.setForeground(Color.white);
        this.add(jLabelBack);
        jLabelBack.setFont(new Font("宋体", Font.BOLD, 25));
        backBtn = new JButton(backImage);
        backBtn.setBounds(870, 300, 140, 50);
        backBtn.setOpaque(false);
        this.add(backBtn);
        backBtn.addActionListener(this);//建立联系
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == transferBtn) {
            try {
                otherAccount = iptTransferUser.getText();
                int id = frame.getLoginUser().getId();
                transferMoney = Double.parseDouble(iptTransferMoney.getText());
                userService.transfer(otherAccount, transferMoney, frame.getLoginUser().getId());
                JOptionPane.showMessageDialog(this, "转账成功");
                iptTransferUser.setText("");
                iptTransferMoney.setText("");
                frame.switchPanel("menu");
            } catch (Exception exception) {
                JOptionPane.showMessageDialog(this, exception.getMessage());
            }
//            String otherAccount = iptTransferUser.getText();
//            JDBCUtil jdbcUtil = new JDBCUtil();
//            UserDO loginUserDO = jdbcUtil.getById(frame.getLoginUser().getId());
//            UserDO otherUserDO = jdbcUtil.getByUserAccount(otherAccount);
//            try {
//                transferMoney = Double.parseDouble(iptTransferMoney.getText()); //输入转账的金额
//            } catch (NumberFormatException exception) {
//                JOptionPane.showMessageDialog(this, "输入金额为空，或格式错误");
//                return;
//            }
//            // 判断账号是否存在
//            if (otherUserDO == null) {
//                JOptionPane.showMessageDialog(this, "查无此人");
//                return;
//            }
//            // 判断是否转给自己
//            if (loginUserDO.getAccount().equals(otherAccount)) {
//                JOptionPane.showMessageDialog(this, "不能转给自己");
//                return;
//            }
//            //判断余额是否为100的倍数
//            if (transferMoney % 100 != 0) {
//                JOptionPane.showMessageDialog(this, "请输入100的倍数");
//                return;
//            }
//            // 判断余额是否足够
//            if (transferMoney > loginUserDO.getBalance()) {
//                JOptionPane.showMessageDialog(this, "余额不足");
//                return;
//            }
////            boolean bl = jdbcUtil.testTransition(loginUser,otherUser);
////            if (bl){
////                // 当前账户扣钱
////                loginUser.setBalance(loginUser.getBalance() - transferMoney);
////                // 对方账户加钱
////                otherUser.setBalance(otherUser.getBalance() + transferMoney);
////                jdbcUtil.update(loginUser);
////                jdbcUtil.update(otherUser);
////                JOptionPane.showMessageDialog(this, "转账成功");
////                iptTransferUser.setText("");
////                iptTransferMoney.setText("");
////                frame.switchPanel("menu");
////            }else {
////                return;
////            }
//            // 当前账户扣钱
//            loginUserDO.setBalance(loginUserDO.getBalance() - transferMoney);
//            //对方账户加钱
//            otherUserDO.setBalance(otherUserDO.getBalance() + transferMoney);
//            try {
//                jdbcUtil.getConnection();
//                jdbcUtil.setAutoCommit(false);
//                jdbcUtil.update(loginUserDO);
//                jdbcUtil.update(otherUserDO);
//                jdbcUtil.commit();
//                JOptionPane.showMessageDialog(this, "转账成功");
//                iptTransferUser.setText("");
//                iptTransferMoney.setText("");
//                frame.switchPanel("menu");
//            } catch (Exception e1) {
//                jdbcUtil.rollback();
//                JOptionPane.showMessageDialog(this, "转账失败");
//            } finally {
//                jdbcUtil.close();
//            }
        } else if (e.getSource() == backBtn) {
            frame.switchPanel("menu");
            iptTransferUser.setText("");
            iptTransferMoney.setText("");
        }
    }


}



